const { LinValidator, Rule } = require('../core/lin-validator')
const { User } = require('../models/user')
const { loginType } = require('../lib/enum')
// const { validator } = require('sequelize/types/lib/utils/validator-extras')
const { NotFound, AuthFailed } = require('../core/http-exception')
// 校验参数是否为正整数
class regValidator extends LinValidator {
  constructor() {
    // new Rule ('isNotEmpty', '必须传入参数')
    super()
    // this.phone = [
    //   // 这里可以添加多个校验规则，但是规则是且的关系
    //   // 三个参数：第一个参数：需要满足的规则，第二个参数：提示信息，第三个参数：可选参数
    //   new Rule('isNotEmpty', '手机号格式不正确'),
    // ]
    this.password = [
      new Rule('isLength', '密码最少6个字符,最多32个字符', {
        min: 6,
        max: 32,
      }),
    ]
    this.nickName = [
      new Rule('isLength', '昵称长度不规范', {
        min: 2,
        max: 10,
      }),
    ]
  }
  async validatePhone(vals) {
    const phone = vals.body.phone
    const user = await User.findOne({
      where: {
        phone,
      },
    })
    if (user) {
      throw new Error('phone已存在')
      // throw new AuthFailed('phone已存在')
    } else {
      return true
    }
  }
}
class TokenValdate extends LinValidator {
  constructor() {
    super()
    this.phone = [
      new Rule('isLength', '不符合账号规则', {
        min: 2,
        max: 100,
      }),
    ]
  }
  async validateLoginType(vals) {
    if (!vals.body.type) {
      throw new Error('登录类型必填')
    }
    if (!loginType.isLoginType(vals.body.type)) {
      throw new Error('登录类型不存在')
    }
  }
}

module.exports = { regValidator, TokenValdate }
